package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.base.Header;
import cc.rengu.igas.bsps.facade.request.GenerateRandomStringRequest;
import cc.rengu.igas.bsps.facade.response.GenerateRandomStringResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 生成随机字符串.
 */
public class GenerateRandomStringService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GenerateRandomStringRequest generateRandomStringRequest = new GenerateRandomStringRequest();
        ConvertUtil.convertOutput(generateRandomStringRequest);
        return generateRandomStringRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BizResponse<GenerateRandomStringResponse> bizResponse = new BizResponse<>();
        GenerateRandomStringRequest generateRandomStringRequest = (GenerateRandomStringRequest) request;
        GenerateRandomStringResponse generateRandomStringResponse = new GenerateRandomStringResponse();
        String instId = generateRandomStringRequest.getHeader().getInstId();
        String userId = generateRandomStringRequest.getUserId();
        String randomType = generateRandomStringRequest.getRandomType();
        String randomLength = generateRandomStringRequest.getRandomLength();
        Header header = generateRandomStringRequest.getHeader();
        UserSessionInfo keySessionInfo = JSON.parseObject(RedisUtil.onceGet(header.getSession()), UserSessionInfo.class);

        int randomLengthNum = StringUtil.isEmptyOrNull(randomLength) ? 32 : Integer.parseInt(randomLength);
        String randomString = "";
        if ("00".equals(randomType) || StringUtil.isEmptyOrNull(randomType)) {
            randomString = RandomUtil.randomString(randomLengthNum);
        } else if ("01".equals(randomType)) {
            randomString = RandomUtil.randomNumStr(randomLengthNum);
        }
        /* 存入redis缓存 */
        keySessionInfo.setRandomString(randomString);
        RedisUtil.onceSet(header.getSession(), JSONObject.toJSONString(keySessionInfo), BspsAppParamConstant.SESSION_EFFECTIVE_TIME);
        generateRandomStringResponse.setRandomString(randomString);
        /* 拼装响应结果 */
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(generateRandomStringResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GenerateRandomStringResponse generateRandomStringResponse = (GenerateRandomStringResponse) bizResponse.getResult();
        ConvertUtil.convertInput(generateRandomStringResponse);
    }
}
